import CryptoJS from "crypto-js";
import axios from "axios";

export default class CameraUtils {
    constructor(pageTitle, viewX, viewY) {
        // 初始化
        this.ip = "10.183.64.101";
        this.userName = "yjzh";
        this.password = "Yjzh@123456";
        this.pageTitle = pageTitle;
        this.viewX = viewX;
        this.viewY = viewY;
        return this;
    }

    // 播放摄像头
    startCamera(fn) {
        // 登录
        this.login(fn);
    }

    // 登录
    login(fn) {
        let sca = 1;
        axios({
            method: "POST",
            url: `http://${this.ip}:8088/VIID/login/v2`,
            headers: {
                "Content-Type": "application/json; charset=utf8",
            },
            responseType: "json",
        })
            .then(res => {
                console.log("Camera LoginInfo:", res);
                const AccessCode = res.data.AccessCode; //res.body改为res.data

                let passwordMd5 = CryptoJS.MD5(this.password).toString();
                let userNameBase64 = CryptoJS.enc.Base64.stringify(CryptoJS.enc.Utf8.parse(this.userName));
                let LoginSignature = CryptoJS.MD5(userNameBase64 + AccessCode + passwordMd5).toString();

                axios({
                    method: "POST",
                    url: `http://${this.ip}:8088/VIID/login/v2`,
                    headers: {
                        "Content-Type": "application/json; charset=utf8",
                    },
                    responseType: "json",
                    data: {
                        UserName: this.userName,
                        AccessCode,
                        LoginSignature,
                    },
                }).then(res => {
                    console.log("Camera Login Operation:", res);
                    let script = document.createElement("script");
                    // ***必须动态引入播放器库文件，以免第三方运维成本上升***
                    script.src = `http://${this.ip}:8093/static/imosPlayer.log.js`;
                    // **B3358P100H50以后可用以下url，方便第三方进行网络代理**
                    // script.src = `http://${ipaddr}${linkPort && ":" + linkPort}/vdapi/static/imosPlayer.min.js`
                    document.body.appendChild(script);
                    script.onload = () => {
                        // 悬浮方案要求网页title唯一，参照接口文档【悬浮播放器概念】
                        const token = res.data.AccessToken;
                        window.token = res.data.AccessToken;
                        // keepalive(token);
                        // 跨nat映射时，如果对接端口不是一一映射到8093，则本接口必调。
                        // 或https代理的对接端口不是443，本接口必调
                        // 本接口必须在init之前调用
                        window.imosPlayer.setLinkPort(8093);
                        window.imosPlayer
                            .init({
                                ip: this.ip, // 必传
                                token: token, // 必传
                                title: this.pageTitle, // 必传
                                offset: [parseInt(this.viewX), parseInt(this.viewY)],
                            })
                            .then(res => {
                                if (res.ErrCode === 0) {
                                    window.imosPlayer.cssScale(sca);
                                    window.imosPlayer.setLiveNetLinkMode("tcp", true);
                                    window.imosPlayer.setReplayNetLinkMode("tcp ", true);
                                    // 国产电脑坐标计算
                                    // window.imosPlayer.setWindowParams(windowParams);
                                    window.__login = true;
                                    fn && fn();
                                } else {
                                    alert(res.ErrMsg);
                                }
                            })
                            .catch(err => {
                                console.error(err);
                            });
                    };
                    script.onerror = e => {
                        console.error(e);
                    };
                });
            })
            .catch(e => console.log(e));
    }

    /**
     * 随机码
     */
    static getRandom() {
        function S4() {
            return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
        }
        return S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4();
    }
}
